package com.ruoyi.resource.dubbo;

import com.alibaba.csp.sentinel.adapter.dubbo3.config.DubboAdapterGlobalConfig;
import com.alibaba.csp.sentinel.adapter.dubbo3.fallback.DefaultDubboFallback;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.oss.core.OssClient;
import com.ruoyi.common.oss.entity.UploadResult;
import com.ruoyi.common.oss.factory.OssFactory;
import com.ruoyi.resource.api.RemoteFileService;
import com.ruoyi.resource.api.domain.SysFile;
import com.ruoyi.resource.domain.SysOss;
import com.ruoyi.resource.mapper.SysOssMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

/**
 * 文件请求处理
 *
 * @author Lion Li
 */
@Slf4j
@Service
@DubboService
public class RemoteFileServiceImpl implements RemoteFileService {

    @Autowired
    private SysOssMapper sysOssMapper;


    /**
     * 文件上传请求
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public SysFile upload(String name, String originalFilename, String contentType, byte[] file) throws ServiceException {
        try {
//            String suffix = StringUtils.substring(originalFilename, originalFilename.lastIndexOf("."), originalFilename.length());
//            OssClient storage = OssFactory.instance();
//            UploadResult uploadResult = storage.uploadSuffix(file, suffix, contentType);
//            // 保存文件信息
//            SysOss oss = new SysOss();
//            oss.setUrl(uploadResult.getUrl());
//            oss.setFileSuffix(suffix);
//            oss.setFileName(uploadResult.getFilename());
//            oss.setOriginalName(originalFilename);
//            oss.setService(storage.getConfigKey());
//            sysOssMapper.insert(oss);
//            SysFile sysFile = new SysFile();
//            sysFile.setOssId(oss.getOssId());
//            sysFile.setName(uploadResult.getFilename());
//            sysFile.setUrl(uploadResult.getUrl());
//            return sysFile;
            log.info("访问了远程Resource接口！");
            return null;
        } catch (Exception e) {
            log.error("上传文件失败", e);
            throw new ServiceException("上传文件失败");
        }
    }

}
